<?php 
	include('connection.php');
	include_once('session.php');
	include_once('../functions/categories.php');
	include_once('../functions/posts.php');

	if ($_POST) {
		$id = $_POST['id'];
		$title = $_POST['title'];
		$content = $_POST['content'];
		$cat_id = $_POST['cat_id'];
		$state = $_POST['state'];
		//Metadata
		$post_meta = $_POST['post_meta'];
		$post_robots = $_POST['post_robots'];
		$meta_title = $_POST['meta_title'];
		$meta_description = $_POST['meta_description'];
		$created_by = $session_name;
		if (empty($title) || empty($content)) {
			echo -1;
		}
		else {
			$content = strip_tags($content);
			$content = mysql_real_escape_string($content);
			//Update post
			if ($id !== '0') {
				$res = update_post($id,$title,$content,$cat_id,$state,$post_meta,$post_robots,$meta_title,$meta_description);
				if ($res == 1) {
					$_SESSION['message'] = "The post is updated successfull";
				}
				echo $res;
			}
			//Insert new post
			else {
				$res = insert_post($title,$content,$cat_id,$state,$post_meta,$post_robots,$meta_title,$meta_description,$created_by);
				if ($res > 0) {
					$_SESSION['message'] = "The post is inserted successfull";
				}
				echo $res;	
			}
		}
	}
 ?>